Add an option to fire http status events only when a change occurs. (#1582)

Darren Cauthon 7 years ago
parent
commit
29007c9019
2 changed files with 50 additions and 2 deletions
  1. 8 2
      app/models/agents/http_status_agent.rb
  2. 42 0
      spec/controllers/http_status_agent_spec.rb

+ 8 - 2
app/models/agents/http_status_agent.rb

@@ -11,7 +11,8 @@ module Agents
11 11
     default_schedule "every_12h"
12 12
 
13 13
     form_configurable :url
14
-    form_configurable :disable_redirect_follow, type: :array, values: ['true', 'false']
14
+    form_configurable :disable_redirect_follow, type: :boolean
15
+    form_configurable :changes_only, type: :boolean
15 16
     form_configurable :headers_to_save
16 17
 
17 18
     description <<-MD
@@ -20,6 +21,8 @@ module Agents
20 21
       Specify a `Url` and the Http Status Agent will produce an event with the HTTP status code. If you specify one or more `Headers to save` (comma-delimited) as well, that header or headers' value(s) will be included in the event.
21 22
 
22 23
       The `disable redirect follow` option causes the Agent to not follow HTTP redirects. For example, setting this to `true` will cause an agent that receives a 301 redirect to `http://yahoo.com` to return a status of 301 instead of following the redirect and returning 200.
24
+
25
+      The `changes only` option causes the Agent to report an event only when the status changes. If set to false, an event will be created for every check.  If set to true, an event will only be created when the status changes (like if your site goes from 200 to 500).
23 26
     MD
24 27
 
25 28
     event_description <<-MD
@@ -72,11 +75,14 @@ module Agents
72 75
       # Track time
73 76
       measured_result = TimeTracker.track { ping(url) }
74 77
 
78
+      current_status = measured_result.result ? measured_result.status.to_s : ''
79
+      return if options['changes_only'] == 'true' && current_status == memory['last_status'].to_s
80
+
75 81
       payload = { 'url' => url, 'response_received' => false, 'elapsed_time' => measured_result.elapsed_time }
76 82
 
77 83
       # Deal with failures
78 84
       if measured_result.result
79
-        payload.merge!({ 'response_received' => true, 'status' => measured_result.status.to_s })
85
+        payload.merge!({ 'response_received' => true, 'status' => current_status })
80 86
         # Deal with headers
81 87
         if local_headers.present?
82 88
           header_results = measured_result.result.headers.select {|header, value| local_headers.include?(header)}

+ 42 - 0
spec/controllers/http_status_agent_spec.rb

@@ -147,6 +147,48 @@ describe 'HttpStatusAgent' do
147 147
         expect(agent.the_created_events[0][:payload]['headers']).to be_nil
148 148
       end
149 149
 
150
+      describe "but the last status code was 200" do
151
+        before { agent.memory['last_status'] = '200' }
152
+
153
+        describe "and no duplication settings have been set" do
154
+          it "should create one event" do
155
+            agent.receive events
156
+            expect(agent.the_created_events.count).to eq(1)
157
+          end
158
+        end
159
+
160
+        describe "and change settings have been set to true" do
161
+          before { agent.options['changes_only'] = 'true' }
162
+          it "should NOT create any events" do
163
+            agent.receive events
164
+            expect(agent.the_created_events.count).to eq(0)
165
+          end
166
+
167
+          describe "but actually, the ping failed" do
168
+
169
+            let(:failing_url)    { SecureRandom.uuid }
170
+            let(:event_with_a_failing_ping)    { Event.new.tap { |e| e.payload = { url: failing_url, headers_to_save: "" } } }
171
+            let(:events) do
172
+              [event_with_a_successful_ping, event_with_a_failing_ping]
173
+            end
174
+
175
+            it "should create an event" do
176
+              agent.receive events
177
+              expect(agent.the_created_events.count).to eq(1)
178
+            end
179
+          end
180
+        end
181
+
182
+        describe "and change settings have been set to false" do
183
+          before { agent.options['changes_only'] = 'false' }
184
+          it "should create one event" do
185
+            agent.receive events
186
+            expect(agent.the_created_events.count).to eq(1)
187
+          end
188
+        end
189
+
190
+      end
191
+
150 192
       describe "but the status code is not 200" do
151 193
         let(:status_code) { 500 }
152 194